const clouddb = require('@hw-agconnect/database-server/dist/index');
const agconnect = require('@hw-agconnect/common-server');
const path = require('path');
const TTrafficSigns = require('./t_traffic_signs');

/**
 * 配置区域
 */
const credentialPath = "/resources/agc-apiclient-1346287091616605888-7331172593826508013.json";
let zoneName = "dtstorage";
let objectName = "t_traffic_signs";

let logger;
let mCloudDBZone;

class CloudDBZoneWrapper {
    // AGC & 数据库初始化
    constructor(log) {
        logger = log;
        let agcClient;
        try {
            agcClient = agconnect.AGCClient.getInstance();
        } catch (err) {
            agconnect.AGCClient.initialize(agconnect.CredentialParser.toCredential(path.join(__dirname, credentialPath)));
            agcClient = agconnect.AGCClient.getInstance();
        }
        // 通过initialize方法初始化AGConnectCloudDB实例
        clouddb.AGConnectCloudDB.initialize(agcClient);
        const agconnectCloudDB = clouddb.AGConnectCloudDB.getInstance(agcClient);
        // 创建CloudDBZoneConfig配置对象并打开储存区
        const cloudDBZoneConfig = new clouddb.CloudDBZoneConfig(zoneName);
        mCloudDBZone = agconnectCloudDB.openCloudDBZone(cloudDBZoneConfig);
    }

    async queryAllByParam(data) {
        if (!mCloudDBZone) {
            logger.info("CloudDBClient is null, try re-initialize it.");
            return;
        }
        try {
            // 根据业务需要修改查询条件
            const cloudDBZoneQuery = this.setQuery(data);
            const resp = await mCloudDBZone.executeQuery(cloudDBZoneQuery);
            return resp.getSnapshotObjects();
        } catch (err) {
            logger.error(`query all by param => ${JSON.stringify(err)}`);
        }
    }

    setQuery(data) {
        const cloudDBZoneQuery = clouddb.CloudDBZoneQuery.where(TTrafficSigns.t_traffic_signs);

        if ("contains" in data) {
            let contains = data.contains;
            for (var key in contains) {
                cloudDBZoneQuery.contains(key, contains[key]);
            }
        }

        if ("equalTo" in data) {
            let equalTo = data.equalTo;
            for (var i in equalTo) {
                cloudDBZoneQuery.equalTo(i, equalTo[i]);
            }
        }

        if ("notEqualTo" in data) {
            let notEqualTo = data.notEqualTo;
            for (var i in notEqualTo) {
                cloudDBZoneQuery.notEqualTo(i, notEqualTo[i]);
            }
        }

        if ("greaterThan" in data) {
            let greaterThan = data.greaterThan;
            for (var i in greaterThan) {
                cloudDBZoneQuery.greaterThan(i, greaterThan[i]);
            }
        }

        if ("greaterThanOrEqualTo" in data) {
            let greaterThanOrEqualTo = data.greaterThanOrEqualTo;
            for (var i in greaterThanOrEqualTo) {
                cloudDBZoneQuery.greaterThanOrEqualTo(i, greaterThanOrEqualTo[i]);
            }
        }

        if ("lessThan" in data) {
            let lessThan = data.lessThan;
            for (var i in lessThan) {
                cloudDBZoneQuery.lessThan(i, lessThan[i]);
            }
        }

        if ("lessThanOrEqualTo" in data) {
            let lessThanOrEqualTo = data.lessThanOrEqualTo;
            for (var i in lessThanOrEqualTo) {
                cloudDBZoneQuery.lessThanOrEqualTo(i, lessThanOrEqualTo[i]);
            }
        }

        if ("in" in data) {
            let funcIn = data.in;
            for (var i in funcIn) {
                cloudDBZoneQuery.in(i, funcIn[i]);
            }
        }

        if ("beginsWith" in data) {
            let beginsWith = data.beginsWith;
            for (var i in beginsWith) {
                cloudDBZoneQuery.beginsWith(i, beginsWith[i]);
            }
        }

        if ("endsWith" in data) {
            let endsWith = data.endsWith;
            for (var i in endsWith) {
                cloudDBZoneQuery.endsWith(i, endsWith[i]);
            }
        }

        if ("isNull" in data) {
            let isNull = data.isNull;
            cloudDBZoneQuery.isNull(isNull);
        }

        if ("isNotNull" in data) {
            let isNotNull = data.isNotNull;
            cloudDBZoneQuery.isNotNull(isNotNull);
        }
        return cloudDBZoneQuery;
    }
}

module.exports = CloudDBZoneWrapper;